var appPath = getAppPath();	

$(function(){
	
//	$("#queryTmplType").combobox({
//		url:appPath+'/itbTmpl/getTypeForCombox.action?category=1',
//		valueField:'type_id',
//		textField:'type_name'
//	});
	
	initJspGrid();
});

// 样版列表初始化
function initJspGrid(){
	
	$('#tmplGrid').datagrid({
	    url:appPath + "/itbTmpl/getTmplList.action",
	    height:'full',
	    //remoteSort:false,
	    striped:true,
	    fitColumns:true,
	    queryParams: getQueryParam(),
	    singleSelect:false,
	    // If set to true, clicking a checkbox will always select the row. 
	    // If false, selecting a row will not check the checkbox.
	    selectOnCheck:true,
	    // If true, the checkbox is checked/unchecked when the user clicks on a row. 
	    // If false, the checkbox is only checked/unchecked when the user clicks exactly on the checkbox.
	    checkOnSelect:true,
	    frozenColumns:[[
	        {field:'ck',checkbox:'true',width:10,align:'center',sortable:false}
	    ]],
	    columns:[[
			{field:'editTmpl',title:'编辑样版',width:40,align:'center',sortable:false,
				formatter:function(value,rowData,rowIndex){
					if(!rowData.tmpl_id) return "";
					var ret = "<a href='javascript:void(0)'><img width='12px' height='12px' style='border:0px' src='"+appPath+"/js/lib/jquery-easyui/themes/icons/pencil.png' title='编辑样版' ";
					ret += " onClick='editTmpl("+rowData.tmpl_id+","+rowData.publish+")'/></a>";
					return ret;
				}
			},
	        {field:'tmpl_id',title:'样版ID',width:45,align:'left',sortable:false},
	        {field:'master',title:'区分',width:40,align:'left',sortable:false,
		    	  formatter:function(value, rowData, rowIndex){
		    		  if(rowData.master == 1 || rowData.master == '1') return '<span style="color:red">样版</span>';
		    		  return '模版';
		    	  }
		      },
//	        {field:'base_tmpl_id',title:'样版ID',width:45,align:'left',sortable:false},
	        {field:'order',title:'显示顺序',width:40,align:'center',sortable:false,
		    	  formatter:function(value, rowData, rowIndex){
		    		  if(!rowData.tmpl_id) return "";
		    		  return "<a href='javascript:void(0)' onClick='editOrder("+rowData.tmpl_id+","+rowData.order+")'>"+rowData.order+"</a>";
		    	  }
		      },
	        {field:'editType',title:'样版分类',width:40,align:'center',sortable:false,
		    	  formatter:function(value, rowData, rowIndex){
		    		  if(!rowData.tmpl_id) return "";
		    		  return "<a href='javascript:void(0)'><img width='12px' height='12px' style='border:0px' src='"+appPath+"/js/lib/jquery-easyui/themes/icons/tip.png' title='编辑分类' onClick='editType("+rowData.tmpl_id+")'/></a>";
		    	  }
		      },
	        {field:'tmpl_name',title:'样版名称',width:150,align:'left',sortable:false},
	        {field:'publish',title:'制作状态',width:45,align:'center',sortable:false,
	        	formatter:function(publish, rowData, rowIndex){
	        		if (publish == 1 || publish =="1") return '已完成';
	        		return '制作中';
	        	}}, 
	        {field:'create_time',title:'创建时间',width:90,align:'center',sortable:false},
	        {field:'update_time',title:'更新时间',width:90,align:'center',sortable:false}
	    ]],
	    hideColumn:['user_type'],
	    //view: imgView,// 自定义View
	    pagination:true,
		rownumbers:true,
		toolbar:[{
			text:'添加样版',
			iconCls:'icon-add',
			handler:function(){
				addTmpl();
			}
		},
		{
			text:'发布样版',
			iconCls:'icon-ok',
			handler:function(){
				doPublish();
			}
		},
		{
			text:'取消发布',
			iconCls:'icon-cancel',
			handler:function(){
				cancelPublish();
			}
		},
		{
			text:'删除样版',
			iconCls:'icon-cancel',
			handler:function(){
				delTmpl();
			}
		}]
	    
	});

}


/**
 * 添加样版
 */
function addTmpl(){

//	$("#addFrame").attr("src", appPath + "/js/egret/index.html");
//	$('#addDlg').window({
//		title:'新建样版',
//		iconCls:'icon-add',
//		modal: true,
//		shadow: true,
//		collapsible:false,
//		minimizable:false,
//		maximizable:false
//	});
//	
//	$('#addDlg').window('move',{top:20,left:20});
//	$('#addDlg').window('open');
	
	var egretUrl = appPath+"/js/egret/index.html";
	window.open(egretUrl);
}

/**
 * 编辑样版
 */
function editTmpl(docId, publish){
	/*var selectTmpl=$("#tmplGrid").datagrid("getChecked");
	if(!selectTmpl || selectTmpl.length != 1){
		$.messager.alert('提示信息','请选择一个样版进行编辑！','warning');
		return;
	}
	
	var docId = "";
	$.each(selectTmpl, function(index, item){
		docId = item.doc_id;
		return false;
	});*/
	
	//$("#editFrame").attr("src", appPath + "/itbTmplWeb/editTmplInit.action?id="+ tmplId);
//	$("#editFrame").attr("src", appPath + "/js/egret/index.html?docId="+docId);
//	$('#editDlg').window({
//		title:'编辑样版',
//		iconCls:'icon-edit',
//		modal: true,
//		shadow: true,
//		collapsible:false,
//		minimizable:false,
//		maximizable:false
//	});
//	
//	$('#editDlg').window('move',{top:20,left:20});
//	$('#editDlg').window('open');
	
	if (publish == 1 || publish =="1") {
		$.messager.alert('提示信息','请勿编辑已发布样版！','warning');
	} else {
		var egretUrl = appPath+"/js/egret/index.html?docId="+docId;
		window.open(egretUrl);
	}
}

/**
 * 显示顺序
 */
function editOrder(docId, order){
	$("#docId").val(docId);
	$("#order").val(order);
	
	$("#orderBtn").show();
	$('#orderDlg').window({
		title:'样版显示顺序设定',
		iconCls:'icon-edit',
		modal: true,
		shadow: true,
		collapsible:false,
		minimizable:false,
		maximizable:false
	});
	
	$('#orderDlg').window('move',{top:20,left:20});
	$('#orderDlg').window('open');
}
/**
 * 修改样版显示顺序
 */
function doUpdOrder(){
	var docId = $("#docId").val();
	var order = $("#order").val();
	if(!order || order.length == 0 || Number(order)>999 || Number(order)<=0){
		$.messager.alert('提示信息','有效数值范围为：1～999','warning');
		return;
	}
	
	var url = appPath+"/itbTmplWeb/updTmplOrder.action?docId="+docId+"&order="+order;
	$.post(url,function(data){
		if(data=="ok"){
			$.messager.alert('提示信息','显示顺序已更新！','info',function(){
				doQuery();
				$('#orderDlg').window('close');
			});
		}else {
			$.messager.alert('提示信息','显示顺序更新失败！','warning');
		}
	});
}

/**
 * 结束样版显示顺序编辑
 */
function cancelUpdOrder(){
	$('#orderDlg').window('close');
}

/**
 * 样版分类
 */
function editType(docId){
	
	$("#docId").val(docId);
	$("#typeBtn").show();
	$('#typeDlg').window({
		title:'样版分类设定',
		iconCls:'icon-edit',
		modal: true,
		shadow: true,
		collapsible:false,
		minimizable:false,
		maximizable:false
	});
	
	$('#typeDlg').window('move',{top:20,left:20});
	$('#typeDlg').window('open');
	
	initChk(docId);
}

function initChk(docId){
	$("input[name='tmplTypeInfo']").prop("checked", false);
	
	// TODO
//	var url = appPath+"/itbTmpl/getTmplType.action?docId="+docId;
//	$.post(url,function(data){
//		if (data != null && data.length>0) {
//			for (i in data) {
//				var id = data[i];
//				$("#typeId"+ id).prop('checked', true);
//			}
//		}
//	});
}

/**
 * 修改样版分类信息
 */
function doPostData(){
	var typeIds = $("input[name='tmplTypeInfo']:checked").map(function(){
        	return $(this).val();
        }).get().join(',');
	
	var docId = $("#docId").val();
	// TODO
//	var url = appPath+"/itbTmplWeb/updTmplType.action?docId="+docId+"&typeIds="+typeIds;
//	$.post(url,function(data){
//		if(data=="ok"){
//			$.messager.alert('提示信息','样版分类已更新！','info');
//		}
//	});
}

/**
 * 结束样版分类编辑
 */
function cancelPostData(){
	$('#typeDlg').window('close');
}

/**
 * 发布样版
 */
function doPublish(){
	var selectTmpl=$("#tmplGrid").datagrid("getChecked");
	if(!selectTmpl || selectTmpl.length == 0){
		$.messager.alert('提示信息','请选择未发布样版进行处理！','warning');
		return;
	}else if(selectTmpl.length>1){
		$.messager.alert('提示信息','请选择单个样版进行处理！','warning');
		return;
	}
	
	var ids = "";
	var doFlg = false;
	$.each(selectTmpl, function(index, item){
		if(item.publish != 1 && item.publish != '1'){
			doFlg = true;
			ids = ids + item.tmpl_id + ",";
		}
	});
	
	if(!doFlg){
		$.messager.alert('提示信息','请选择未发布样版进行处理！','warning');
		return;
	}
	
	$.messager.confirm("确认",
		"是否确认发布样版？<br/><span style='color:red'>#请确保样版已经过认真检查</span>",
		function (r) {
	        if (r) {
	        	var url = appPath+"/itbTmplWeb/doPublish.action?ids="+ids;
	        	$.post(url,function(data){
	        		if(data=="ok"){
	        			$.messager.alert('提示信息','样版发布成功！','info');
	        			doQuery();
	        		} else if (data=="ng"){
	        			$.messager.alert('提示信息','所选文档存在未生成预览图的文档页，请先生成预览！','warning');
	        		}
	        	});
	        }
		}
	);
}

/**
 * 取消发布
 */
function cancelPublish(){
	var selectTmpl=$("#tmplGrid").datagrid("getChecked");
	if(!selectTmpl || selectTmpl.length == 0){
		$.messager.alert('提示信息','请选择已发布样版进行处理！','warning');
		return;
	}
	
	var ids = "";
	var doFlg = false;
	$.each(selectTmpl, function(index, item){
		if(item.publish == 1 || item.publish == '1'){
			doFlg = true;
			ids = ids + item.tmpl_id + ",";
		}
	});
	
	if(!doFlg){
		$.messager.alert('提示信息','请选择已发布样版进行处理！','warning');
		return;
	}
	
	$.messager.confirm("确认", "是否确认取消已发布样版？", function (r) {
		if(r){
			var url = appPath+"/itbTmplWeb/cancelPublish.action?ids="+ids;
			$.post(url,function(data){
				if(data=="ok"){
					$.messager.alert('提示信息','样版已取消发布！','info');
					doQuery();
				}
			});
		}
	});
}

/**
 * 删除样版
 */
function delTmpl(){
	var selectItem=$("#tmplGrid").datagrid("getChecked");
	if(!selectItem || selectItem.length == 0){
		$.messager.alert('提示信息','请选择样版','warning');
		return;
	}
	$.messager.confirm("确认", "是否确认删除", function (r) {  
        if (r) { 
			var ids = "";
			$.each(selectItem, function(index, item){
				ids = ids + item.tmpl_id + ",";
			});
			var url = appPath+"/itbTmplWeb/deleteTmpl.action?ids="+ids;
			$.post(url,function(data){
				if(data=="ok"){
					$.messager.alert('提示信息','删除成功','info');
					doQuery();
				}
			});
        }
	});
}

/**
 * 获取请求信息
 */
function getQueryParam(){
	var queryObject = {};
	
	var findMstType = $("#queryTmplType").val();
	var findPubFlg = $("#queryPublish").val();
	var findTmplName=$("#queryTmplName").val();
	
	if (findMstType&&findMstType.length>0) {
		queryObject.findMstType = findMstType;// 样版类型 TODO
	}
	if(findPubFlg&&findPubFlg.length>0) queryObject.findPubFlg = findPubFlg;// 发布标记
	if(findTmplName&&findTmplName.length>0) queryObject.findTmplName = findTmplName;// 样版名称
	queryObject.findMstFlg = 1;// 样版标记
	
	return queryObject;
}

/**
 * 查询数据
 */
function doQuery(){
	$("#tmplGrid").datagrid('unselectAll');
	$("#tmplGrid").datagrid('load', getQueryParam());
}

function fmtCreateTime(value, rowData, rowIndex){
	if(!value)return "";
	var s = new Date(value);
	return rowData.create_time=$.DateUtil.format(s,'yyyy/MM/dd hh:mm:ss');
}
